<script lang="ts" setup>
import type { VbenFormSchema } from '#/adapter/form';
import type { DictDataApi } from '#/api/system/dict/dictdata';
import { gridDictType } from '#/api/system/dict/dicttype';
import { useVbenDrawer } from '@vben/common-ui';
import { useVbenForm, z } from '#/adapter/form';
import { createDictData, updateDictData } from '#/api/system/dict/dictdata';
import { computed, h, ref } from 'vue';
import { $t } from '#/locales';

defineOptions({
  name: 'Dictform',
});
const emit = defineEmits(['success']);

const formData = ref<DictDataApi.DictData>();

const schema: VbenFormSchema[] = [
  {
    component: 'ApiTreeSelect',
    componentProps: {
      allowClear: true,
      api: gridDictType,
      class: 'w-full',
      labelField: 'dictTypeName',
      valueField: 'dictTypeCode',
      childrenField: 'children',
    },
    formItemClass: 'col-span-3 col-start-1',
    fieldName: 'dictTypeCode',
    label: $t('system.dict.dictData.dictTypeCode'),
  },
  {
    component: 'Input',
    fieldName: 'table',
    formItemClass: 'col-span-4 col-start-4',
    label: $t('system.dict.dictData.table'),
  },
  {
    component: 'Input',
    fieldName: 'dictCode',
    formItemClass: 'col-span-2 col-start-1',
    label: $t('system.dict.dictData.dictCode'),
    rules: z
      .string()
      .min(
        2,
        $t('ui.formRules.minLength', [$t('system.dict.dictData.dictCode'), 1]),
      )
      .max(
        30,
        $t('ui.formRules.maxLength', [$t('system.dict.dictData.dictCode'), 30]),
      ),
  },
  {
    component: 'Input',
    fieldName: 'dictName',
    formItemClass: 'col-span-4 col-start-3',
    label: $t('system.dict.dictData.dictName'),
    rules: z
      .string()
      .min(
        2,
        $t('ui.formRules.minLength', [$t('system.dict.dictData.dictName'), 2]),
      )
      .max(
        30,
        $t('ui.formRules.maxLength', [$t('system.dict.dictData.dictName'), 30]),
      ),
  },

  {
    component: 'Input',
    fieldName: 'seq',
    formItemClass: 'col-span-2 col-start-1',
    label: $t('system.dict.dictData.seq'),
    defaultValue: 5,
  },
  {
    component: 'RadioGroup',
    componentProps: {
      buttonStyle: 'solid',
      options: [
        { label: $t('common.enabled'), value: 1 },
        { label: $t('common.disabled'), value: 0 },
      ],
      optionType: 'button',
    },
    defaultValue: 1,
    fieldName: 'status',
    formItemClass: 'col-span-3 col-start-3',
    label: $t('system.menu.status'),
  },
  {
    component: 'Textarea',
    fieldName: 'memo',
    formItemClass: 'col-span-6 items-baseline',
    componentProps: {
      rows: 4,
    },
    label: $t('system.dict.dictData.memo'),
  },
];
const [Form, formApi] = useVbenForm({
  schema,
  // commonConfig: {
  //   colon: true,
  //   formItemClass: 'col-span-2 md:col-span-1',
  // },
  // wrapperClass: 'grid-cols-2 gap-x-4',

  showDefaultActions: false,
  // 所有表单项共用，可单独在表单内覆盖
  commonConfig: {
    // 所有表单项
    componentProps: {
      class: 'w-full',
    },
  },
  // 一共三列
  wrapperClass: 'grid-cols-6',
  layout: 'horizontal',
});
const [Drawer, drawerApi] = useVbenDrawer({
  onCancel() {
    drawerApi.close();
  },
  onConfirm: async () => {
    const { valid } = await formApi.validate();
    if (valid) {
      drawerApi.lock();
      const data = await formApi.getValues();

      try {
        await (formData.value?.dictId
          ? updateDictData(formData.value.dictId, data)
          : createDictData(data));
        drawerApi.close();
        emit('success');
      } finally {
        drawerApi.lock(false);
      }
    }
  },
  onOpenChange(isOpen: boolean) {
    if (isOpen) {
      const data = drawerApi.getData<Record<string, any>>();
      if (data) {
        formData.value = data;
        formApi.setValues(formData.value);
      }
    }
  },
});
const getDrawerTitle = computed(() =>
  formData.value?.dictId
    ? $t('ui.actionTitle.edit', [$t('system.dict.dictData.name')])
    : $t('ui.actionTitle.create', [$t('system.dict.dictData.name')]),
);
</script>
<template>
  <Drawer class="w-full max-w-[800px]" :title="getDrawerTitle">
    <Form class="mx-4" />
  </Drawer>
</template>
